//
//  QandAViewController.m
//  New
//
//  Created by 才诗琦 on 16/1/17.
//  Copyright © 2016年 www.lanou3g.com. All rights reserved.
//

#import "QandAViewController.h"
#import "SliderView-Prefix.pch"
@interface QandAViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, assign)NSInteger number;
@end

@implementation QandAViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    //tableView
    [self createTableView];
    
    
   
    
  
  
}
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 100) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    
   
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dadaHandeler)];
    [self.tableView.mj_header beginRefreshing];
    [self.tableView.mj_header endRefreshing];
    
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadnewdata)];
    if (_dataArr.count != 0) {
        [self.tableView.mj_footer beginRefreshing];
    }
    self.number = 10;

    
    //注册cell
    [_tableView registerClass:[QandACell class] forCellReuseIdentifier:@"cell"];

    //处理数据
    [self data];
    
    [_tableView release];
    
    
    
    
}


#warning 上拉加载
- (void)loadnewdata
{
    [AFNModel GETWithHandler:[NSString stringWithFormat:@"http://c.m.163.com/newstopic/list/expert/%ld-10.html", self.number]  completion:^(id result) {
        NSArray *arrTemp = [NSArray array];
        arrTemp = [QandAModel modelHandlerWithArray:result[@"data"][@"expertList"]];
        for (NSInteger i = 0; i < 10; i++) {
            [self.dataArr addObject:arrTemp[i]];
        }
        [_tableView reloadData];
    }];
    [self.tableView.mj_footer endRefreshing];
    self.number += 10;
}

#warning 下拉刷新
- (void)dadaHandeler
{
    [AFNModel GETWithHandler: @"http://c.m.163.com/newstopic/list/expert/0-10.html"  completion:^(id result) {
        self.dataArr = [NSMutableArray array];
        self.dataArr = [QandAModel modelHandlerWithArray:result[@"data"][@"expertList"]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格
            [self.tableView reloadData];
            
            // 拿到当前的下拉刷新控件，结束刷新状态
            [self.tableView.mj_header endRefreshing];
        });
        
    }];
    
}
#warning 处理数据
- (void)data
{
    NSString *str = @"http://c.m.163.com/newstopic/list/expert/0-10.html";
    
    [AFNModel GETWithHandler:str completion:^(id result) {
        self.dataArr = [QandAModel modelHandlerWithArray:result[@"data"][@"expertList"]];
        [_tableView reloadData];
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _dataArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QandACell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    QandAModel *qa = _dataArr[indexPath.row];
    
    [cell.imagView sd_setImageWithURL:[NSURL URLWithString:qa.picurl]];
    [cell.userView sd_setImageWithURL:[NSURL URLWithString:qa.headpicurl]];
    
    cell.nameLabel.text = qa.name;
    cell.aliasLabel.text = qa.alias;
    cell.concernCountLabel.text = [qa.concernCount stringValue];
    cell.classificationLabel.text = qa.classification;
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QandAModel *q = _dataArr[indexPath.row];
    
    CGFloat h = [QandACell heightWithText:q.alias];
    return  h + 200;
    
    
    
}

//跳转详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    QandAModel *q = _dataArr[indexPath.row];
    LatestVC *xiangQing = [[LatestVC alloc] init];
    
    xiangQing.string = q.expertId;
    
    [self.navigationController pushViewController:xiangQing animated:YES];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
